package healththink

import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(User)
@Mock([User, FacilityUserLink, Facility])
class UserTests {
	
	@Test
    void testUserAddress() {	   
	   User user = HealthThink.getStaticUser()
	   Email email = HealthThink.getStaticEmail()
	   TelephoneNumber tele = HealthThink.getStaticPhone() 
	   Address address = HealthThink.getStaticAddress()
	   
	   assertNotNull("user null?", user)
	   assertNotNull("Address Null?", address)
	   
	   user.addToAddresses(address)
	   user.addToTelephoneNumbers(tele)
	   user.addToAdditionalEmails(email)

	   if(user.validate())
	   {
		   assertNotNull("Save User not null?", user.save(flush:true))
		   user.delete(flush:true)
	   }
	   else
	   {
		   fail("User Not Validated")
	   }
	   
	   Facility f = HealthThink.getStaticFacility()
	   FacilityUserLink ful = HealthThink.getStaticFacilityUserLink()

	   f.addToFacilityUsers(ful)
	   user.addToFacilities(ful)
	   user.save(flush:true)
	  
	   User expected = User.findByUserId(user.userId)
		  
	   assertNotNull("(error)FacilityUserLink Target was not saved", user)
	   assertNotNull("(error)Expected FacilityUserLink was null", expected)
	   assertEquals("(error)Target and Expected are not Equal", user, expected)
	   
	   String userList = user.facilities.toString()
	   String expectedList = expected.facilities.toString()
	   assertEquals("(error)Target ATTR and Expected ATTR are not Equal",
		   user.facilities,
		   expected.facilities)
		  
	   user.delete(flush:true)
	   
    }
}
